﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using ModelExamples;

namespace SquatSimulator
{
  class ShipData : INotifyPropertyChanged
  {
    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    public ShipData(Emulator emul)
    { 
      Order = emul.EngineOrderState;
      Depth = emul.Depth;
      Refresh(emul);
    }

    double _velocity;
    public double Velocity
    {
      get { return _velocity; }
      private set
      {
        _velocity = value;
        Notify();
      }
    }

    double _order;
    public double Order
    {
      get { return _order; }
      set
      {
        _order = value;
        Notify();
      }
    }

    bool _isGrounded;
    public bool IsGrounded
    {
      get { return _isGrounded; }
      private set
      {
        _isGrounded = value;
        Notify();
      }
    }

    double _orderState;
    public double OrderState
    {
      get { return _orderState; }
      private set
      {
        _orderState = value;
        Notify();
      }
    }

    double _pitch;
    public double Pitch
    {
      get { return _pitch; }
      private set
      {
        _pitch = value;
        Notify();
      }
    }

    double _sinkage;
    public double Sinkage
    {
      get { return _sinkage; }
      private set
      {
        _sinkage = value;
        Notify();
      }
    }

    double _bowDraft;
    public double BowDraft
    {
      get { return _bowDraft; }
      private set
      {
        _bowDraft = value;
        Notify();
      }
    }

    double _sternDraft;
    public double SternDraft
    {
      get { return _sternDraft; }
      private set
      {
        _sternDraft = value;
        Notify();
      }
    }

    double _depth;
    public double Depth
    {
      get { return _depth; }
      set
      {
        _depth = value;
        Notify();
      }
    }

    TimeSpan _time = new TimeSpan();
    public TimeSpan Time
    {
      get { return _time; }
      private set
      {
        _time = value;
        Notify();
      }
    }

    public void Refresh(Emulator emul)
    {
      //getting params from emulator
      Time = new TimeSpan(0,0,(int)emul.ManeuverTime);
      Pitch = emul.Pitch;
      Sinkage = emul.Sinkage;
      BowDraft = emul.BowDraft;
      SternDraft = emul.SternDraft;
      Velocity = emul.Velocity;
      OrderState = emul.EngineOrderState;
      IsGrounded = emul.IsGrounded;

      //setting params to emulator
      if(emul.EngineOrder != Order )
        emul.EngineOrder = Order;
      if( emul.Depth != Depth )
        emul.Depth = Depth;
    }

    private void Notify()
    {
      var prop_name = new StackFrame(1).GetMethod().Name.Substring(4);
      Notify(prop_name);
    }
    private void Notify(string property)
    {
      if (PropertyChanged != null)
      {
        PropertyChanged(this, new PropertyChangedEventArgs(property));
      }
    }
  }
}
